package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限查询
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 查询所有权限信息
     *
     * @return
     */
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        try {
            List<Permission> permissionList = permissionService.findAllPermission();
            return new Result(true,"获取权限信息成功",permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取权限信息失败");
        }
    }
    /**
     * 权限的分页，及条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 新建权限
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, "新建权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新建权限失败");
        }
    }

    @RequestMapping("/query")

    public Result query(Integer id){

        try {
            Permission qurey = permissionService.query(id);  //查询成功
            return new Result(true,"查询成功",qurey);
        } catch (Exception e) {  //查询失败
            e.printStackTrace();
            return new Result(false,"查询失败，请刷新页面");

        }
    }


    @RequestMapping("/updateById")
    public Result updateById(@RequestBody Permission permission){
        try {
            permissionService.updateById(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/deleteById")
    public Result DeleteById(Integer id){
        try {
            boolean delete = permissionService.deleteById(id);
            if(delete){
                //删除成功 :
                return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }else {
                //删除失败
                return new Result(false,"该检查项已被其他检查组引用");
            }
        } catch (Exception e) {
            //后台系统出错
            e.printStackTrace();
            return new Result(false,"系统出错了....");

        }
    }
}
